import request from '@/config/axios'

export interface UserVO {
  id: number
  username: string
  nickname: string
  deptId: number
  postIds: string[]
  email: string
  mobile: string
  sex: number
  avatar: string
  loginIp: string
  status: number
  remark: string
  loginDate: Date
  createTime: Date
}

const cache = new Map()

// 查询用户管理列表
export const getUserPage = (params: PageParam) => {
  return request.get({ url: '/system/user/page', params })
}

// 查询用户详情
export const getUser = (id: number) => {
  return request.get({ url: '/system/user/get?id=' + id })
}

// 新增用户
export const createUser = (data: UserVO) => {
  cache.clear()
  return request.post({ url: '/system/user/create', data })
}

// 修改用户
export const updateUser = (data: UserVO) => {
  cache.clear()
  return request.put({ url: '/system/user/update', data })
}

// 删除用户
export const deleteUser = (id: number) => {
  cache.clear()
  return request.delete({ url: '/system/user/delete?id=' + id })
}

// 导出用户
export const exportUser = (params: any) => {
  return request.download({ url: '/system/user/export', params })
}

// 下载用户导入模板
export const importUserTemplate = () => {
  return request.download({ url: '/system/user/get-import-template' })
}

// 用户密码重置
export const resetUserPassword = (id: number, password: string) => {
  cache.clear()
  const data = {
    id,
    password
  }
  return request.put({ url: '/system/user/update-password', data: data })
}

// 用户状态修改
export const updateUserStatus = (id: number, status: number) => {
  cache.clear()
  const data = {
    id,
    status
  }
  return request.put({ url: '/system/user/update-status', data: data })
}

// 获取用户精简信息列表
export const getSimpleUserList = async (): Promise<UserVO[]> => {
  if (cache.has('system_user_simple_list')) {
    return cache.get('system_user_simple_list')
  }
  const res = await request.get({ url: `/system/user/simple-list` })
  cache.set('system_user_simple_list', res)
  return res
  // return request.get({ url: '/system/user/simple-list' })
}

// 获取用户精简信息列表
export const getSimpleAllUserList = async () => {
  if (cache.has('system_user_simple_list_all')) {
    return cache.get('system_user_simple_list_all')
  }
  const res = await request.get({ url: '/system/user/list-all-simple' })
  cache.set('system_user_simple_list_all', res)
  return res
  // return request.get({ url: '/system/user/list-all-simple' })
}

// 获取用户精简信息列表-航机部-司机
export const getSimpleDrvUserList = async () => {
  if (cache.has('system_user_simple_list_drv')) {
    return cache.get('system_user_simple_list_drv')
  }
  const res = await request.get({ url: '/system/user/list-drv-simple' })
  cache.set('system_user_simple_list_drv', res)
  return res
  // return request.get({ url: '/system/user/list-drv-simple' })
}

// 获取用户精简信息列表-航机部-安全员
export const getSimpleSoUserList = async () => {
  if (cache.has('system_user_simple_list_so')) {
    return cache.get('system_user_simple_list_so')
  }
  const res = await request.get({ url: '/system/user/list-so-simple' })
  cache.set('system_user_simple_list_so', res)
  return res
  // return request.get({ url: '/system/user/list-so-simple' })
}

// 获取用户精简信息列表-航机部-航机员
export const getSimpleAmeUserList = async () => {
  if (cache.has('system_user_simple_list_ame')) {
    return cache.get('system_user_simple_list_ame')
  }
  const res = await request.get({ url: '/system/user/list-ame-simple' })
  cache.set('system_user_simple_list_ame', res)
  return res
  // return request.get({ url: '/system/user/list-ame-simple' })
}
